package cn.jsj.invite.controller;

import cn.hutool.core.lang.Pair;
import cn.jsj.invite.interceptor.TokenCensor;
import cn.jsj.invite.interceptor.swich.PassToken;
import cn.jsj.invite.pojo.InvitationInfo;
import cn.jsj.invite.pojo.SchoolAdminEnt;
import cn.jsj.invite.pojo.StudentEnt;
import cn.jsj.invite.pojo.SuperAdminInfo;
import cn.jsj.invite.pojo.identity.UserIdentityEnum;
import cn.jsj.invite.result.Result;
import cn.jsj.invite.service.SuperAdminService;
import com.github.pagehelper.PageInfo;
import org.checkerframework.common.util.report.qual.ReportUnqualified;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
public class SuperAdminController {
    private final SuperAdminService superAdminService;
    private final TokenCensor tokenCensor;

    public SuperAdminController(SuperAdminService superAdminService, TokenCensor tokenCensor) {
        this.superAdminService = superAdminService;
        this.tokenCensor = tokenCensor;
    }

    //超管登录
    @PassToken
    @PostMapping("/admin/login")
    public Result<?> adminLogin(@RequestParam String username, @RequestParam String password) {
        SuperAdminInfo superAdminInfo = superAdminService.login(username, password);
        if (superAdminInfo != null) {
            String token = tokenCensor.tokenIssue(superAdminInfo.getId().longValue(), UserIdentityEnum.SUPER);
            return Result.success(new Pair<>(superAdminInfo.getId().toString(), token));
        }
        return Result.fail("用户名或密码有误");
    }

    //超管根据学校ID查询所有学生
    @GetMapping("/students/{schoolId}")
    public Result<?> studentsSchoolId(@PathVariable Integer schoolId, @RequestParam Integer page) {
        PageInfo<StudentEnt> studentPageInfo = superAdminService.pageStudentsInfo(schoolId, page);
        return Result.success(studentPageInfo);
    }

    //超管添加学校管理员
    @PostMapping("/schoolAdmin")
    public Result<?> addSchoolAdmin(String name, String password, Integer schoolId, String phone) {
        Integer integer = superAdminService.addOneSchoolAdmin(name, password, schoolId, phone);
        if (integer.equals(1)) {
            return Result.success();
        }
        return Result.fail("添加失败");
    }

    /**
     * 管理员修改密码 通过旧密码以及adminID
     *
     * @param adminOldPassword 旧密码
     * @param adminNewPassword 新密码
     * @param adminId          管理员ID
     * @return 1：成功 0：失败
     */
    @RequestMapping("/admin/updatePassword")
    public Result<String> modifyAdminPassword(String adminOldPassword, String adminNewPassword, Integer adminId) {
        if (superAdminService.updatePasswordById(adminOldPassword, adminNewPassword, adminId).equals(1)) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    //超管删除学校管理员
    @PostMapping("/delete/schoolAdmin")
    public Result<?> deleteSchoolAdminByAdminId(Integer schoolAdminId, Integer schoolId) {
        Integer integer = superAdminService.deleteSchoolAdminByAdminId(schoolAdminId, schoolId);
        if (integer == null) {
            return Result.fail("参数无效");
        }
        if (integer.equals(1)) {
            return Result.success();
        } else {
            return Result.fail("删除失败");
        }
    }

    //超级管理员查看所有帖子
    @GetMapping("/adminAll/pageInfo")
    public Result<?> adminAllPageInfo(Integer schoolId, Integer pageNum) {
        PageInfo<InvitationInfo> invitationInfoPageInfo = superAdminService.listAllPageInfoWithSchoolId(schoolId, pageNum);
        if (invitationInfoPageInfo.getSize() != 0) {
            return Result.success(invitationInfoPageInfo);
        }
        return Result.fail("查无数据");
    }

    //超管查看所有帖子
    @PostMapping("/super/findAll/page")
    public Result<?> superFindAllpage(Integer superId) {
        List<InvitationInfo> infos = superAdminService.listAllPageInfo(superId);
        if (infos == null) {
            return Result.fail("参数无效");
        }
        return Result.success(infos);
    }

    //超管查看所有帖子
    @PostMapping("/super/find/student")
    public Result superFindStudent(Integer superId) {
        List<StudentEnt> studentEnts = superAdminService.listAllStudent(superId);
        if (studentEnts == null) {
            return Result.fail("参数无效");
        }
        return Result.success(studentEnts);
    }

    //超管查看所有的学校管理员
    @PostMapping("/super/findAll/schoolAdmin")
    public Result<?> superFindAllSchoolAdmin(Integer superAdminId) {
        List<SchoolAdminEnt> schoolAdminList = superAdminService.listAllSchoolAdmin(superAdminId);
        if (schoolAdminList == null) {
            return Result.fail("信息有误");
        }
        return Result.success(schoolAdminList);
    }

    //超管修改学校管理员的密码
    @PostMapping("/update/schoolAdmin/password")
    public Result<?> updateSchoolAdminPassword(Integer schoolAdminId, String newPassword) {
        Integer integer = superAdminService.updateSchoolAdminPassword(schoolAdminId, newPassword);
        if (integer == null) {
            return Result.fail("参数无效");
        }
        if (integer.equals(1)) {
            return Result.success();
        }
        return Result.fail("修改失败");
    }


}
